<?php
/**
 * PicBoard SVG handler
 *
 * Pulls SVG's down from a remote URL.
 *
 * PHP version 5
 *
 * LICENSE: <LICENCE-HERE>
 *
 * @author     Sebastian Skuse <scs@ecs.soton.ac.uk>
 * @copyright  2009-2010 University of Southampton
 * @license    http://<LICENSE-URL>  <LICENSE-NAME>
 * @version    CVS: $1:$
 */

error_reporting(0);
header("Content-type: image/svg+xml");
//header("Content-type: application/xhtml+xml");

$url = "http://" . urldecode($_GET['r']);
$headers = get_headers($url);

preg_match("/HTTP\/\d\.\d.(\d{3})/", $headers[0], $matches);

// Did we recieve a 200 response code?
if($matches[1] == 200){

	$data = file_get_contents( $url );
		
	// Remove height & width restrictions
	$data = preg_replace("/(<svg[^>]+width=)(.*?)( height=)['\"](.*?)['\"]([^>]+)/is", "$1\"100%\"$3\"100%\"$5", $data);
	//$data = preg_replace("/(<svg[^>]+width=[\"'])([^\"']+)([^>]+)/is", "${1}100%$3"); // width
	//$data = preg_replace("/(<svg[^>]+height=[\"'])([^\"']+)([^>]+)/is", "${1}100%$3"); // height
	
	// Remove rect's 
	$data = preg_replace("/(<rect.*?style=\".*?)(stroke:#000000;)(\".*?>.+<\/svg>.*?)$/is", "$1$3", $data);
	
	echo $data;

} else {
	// Remote image doesn't exist. Rather than just failing lets return a blank SVG.
	echo file_get_contents("images/Unknown.svg");
}

?>